package 多线程;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;

import static 多线程.img.OUTIMG;

/**
 * @Author: 李佳芬
 * @Description: Callable
 * @DateTime: 2023/3/31 10:44
 **/
public class Callable01 implements Callable<Boolean> {

    private String name;
    private  String url;

    public Callable01(String name, String url) {
        this.name = name;
        this.url = url;
    }

    @Override
    public Boolean call() throws Exception {
        String outimg = OUTIMG(url, name);
        System.out.println("下载了文件"+name+"地址为"+outimg);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // new出Callable的实现类对象
        Callable01 callable01 = new Callable01("美女键盘","https://pic.netbian.com/uploads/allimg/230327/194745-16799176658fd9.jpg");
        Callable01 callable02 = new Callable01("刻晴","https://pic.netbian.com/uploads/allimg/230326/010720-16797640409489.jpg");
        Callable01 callable03 = new Callable01("可爱女孩 赛博朋克 街道","https://pic.netbian.com/uploads/allimg/230311/003345-16784660256b9e.jpg");
        // 创建线程池
        ExecutorService service = Executors.newFixedThreadPool(3);

        // 提交服务
        Future<Boolean> s1 = service.submit(callable01);
        Future<Boolean> s2 = service.submit(callable02);
        Future<Boolean> s3 = service.submit(callable03);

        // 获取结果
        Boolean aBoolean = s1.get();
        // 关闭线程
        service.shutdownNow();
    }
}
